/*
 * @(#)AbstractMessageInfo.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved. This software is the confidential and proprietary information of DCIVision Ltd ("Confidential Information").
 * You * shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.dcivision.alert.bean.MtmUpdateAlertRecipient;
import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.alert.dao.MtmUpdateAlertRecipientDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;
/**
 * <p>
 * Class Name: EditMessageDetailAction.java
 * </p>
 * 
 * @author Greatwall shao
 * @company DCIVision Limited
 * @creation date 2006-09-24
 */
public abstract class AbstractMessageInfo implements MessageDetailInfo {

  protected Connection dbConn;
  protected SessionContainer sessionContainer;
  protected MessageInfoBean messageInfoBean = new MessageInfoBean();
  protected UpdateAlertSystemLog alertSystemLog;
  MessageFormatter messageFormatter;

  public void setSessionContainer(SessionContainer session) {
    this.sessionContainer = session;
  }

  public void setConnection(Connection dbConn) {
    this.dbConn = dbConn;
  }

  public AbstractBaseObject getMessageInfo() throws ApplicationException {
    
    setCommonInfo();
    
    setSystemLogInfo();
    
    setInviteeIDList();
    
    setOtherInfo();   
    
    return messageInfoBean;
  }

  private void setInviteeIDList() throws ApplicationException {

    List recipientAry = new ArrayList();
    MtmUpdateAlertRecipientDAObject updateRecipientDAO = new MtmUpdateAlertRecipientDAObject(
        sessionContainer, dbConn);

    if (!Utility.isEmpty(this.alertSystemLog.getUpdateAlertID())) {
      recipientAry = updateRecipientDAO.getByUpdateAlertTypeID(this.alertSystemLog
          .getUpdateAlertTypeID());
    }
    String userIDs = "";

    String groupIDs = "";

    String roleIDs = "";
    MtmUpdateAlertRecipient tmprecipient;
    for (int i = 0; i < recipientAry.size(); i++) {

      tmprecipient = (MtmUpdateAlertRecipient) recipientAry.get(i);

      String recipientID = String.valueOf(tmprecipient.getRecipientID());

      if (tmprecipient.getRecipientType().equals(UpdateAlert.USER_RECIPIENT)) {
        userIDs+=recipientID+",";
      }
      else if (tmprecipient.getRecipientType().equals(UpdateAlert.GROUP_RECIPIENT)) {
        groupIDs+=recipientID+",";
      }
      else if (tmprecipient.getRecipientType().equals(UpdateAlert.ROLE_RECIPIENT)) {
        roleIDs+=recipientID+",";
      }
    }
    this.messageInfoBean.setUserIDList(cutEndComma(userIDs).split(","));
    this.messageInfoBean.setGroupIDList(cutEndComma(groupIDs).split(","));
    this.messageInfoBean.setRoleIDList(cutEndComma(roleIDs).split(","));

  }

  protected abstract void setOtherInfo() throws ApplicationException;

  private void setCommonInfo() {
    this.messageInfoBean.setSystemLogID(this.alertSystemLog.getID().toString());
    this.messageInfoBean.setUpdateAlertID(this.alertSystemLog.getUpdateAlertID().toString());
    this.messageInfoBean.setMessage(this.alertSystemLog.getMessage());
  }

  private void setSystemLogInfo() throws ApplicationException {
    
    
    this.messageInfoBean.setArrivalDate(alertSystemLog.getCreateDate());
  
    if(!Utility.isEmpty(alertSystemLog.getDueDate()))
    {
    this.messageInfoBean.setDueDate(alertSystemLog.getDueDate());
    }
    this.messageInfoBean.setSender(messageFormatter.getSender());
    this.messageInfoBean.setSubject(messageFormatter.getSubjectWithoutLink());
    
  }
/**
 * @deprecated
 * @throws ApplicationException
 */
  private void setInvitee() throws ApplicationException {
  
    UserRoleDAObject userRoleDAO = new UserRoleDAObject(sessionContainer, dbConn);
    UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionContainer, dbConn);
    UserGroupDAObject userGroupDAO = new UserGroupDAObject(sessionContainer, dbConn);
  
    List recipientAry = new ArrayList();
    MtmUpdateAlertRecipientDAObject updateRecipientDAO = new MtmUpdateAlertRecipientDAObject(
        sessionContainer, dbConn);
  
    if (!Utility.isEmpty(this.alertSystemLog.getUpdateAlertID())) {
      recipientAry = updateRecipientDAO.getByUpdateAlertTypeID(this.alertSystemLog.getUpdateAlertTypeID());
    }
    String userRecords = "";
    String userGroups = "";
    String userRoles = "";
    MtmUpdateAlertRecipient tmprecipient;
    for (int i = 0; i < recipientAry.size(); i++) {
  
      tmprecipient = (MtmUpdateAlertRecipient) recipientAry.get(i);
  
      if (tmprecipient.getRecipientType().equals(UpdateAlert.USER_RECIPIENT)) {
        UserRecord userRecord = (UserRecord) userRecordDAO.getObjectByID(tmprecipient
            .getRecipientID());
        userRecords += userRecord.getFullName();
        userRecords += " , ";
      } else if (tmprecipient.getRecipientType().equals(UpdateAlert.GROUP_RECIPIENT)) {
        UserGroup userGroup = (UserGroup) userGroupDAO.getObjectByID(tmprecipient.getRecipientID());
        userGroups += userGroup.getGroupName();
        userGroups += " , ";
      } else if (tmprecipient.getRecipientType().equals(UpdateAlert.ROLE_RECIPIENT)) {
        UserRole userRole = (UserRole) userRoleDAO.getObjectByID(tmprecipient.getRecipientID());
        userRoles += userRole.getRoleName();
        userRoles += " , ";
      }
    }
    this.messageInfoBean.setUserRecords(cutEndComma(userRecords));
    this.messageInfoBean.setUserGroups(cutEndComma(userGroups));
    this.messageInfoBean.setUserRoles(cutEndComma(userRoles));
  
  }

  protected String cutEndComma(String str) {
    if (Utility.isEmpty(str)) {
      return "";
    }
    if (!str.trim().endsWith(",")) {
      return str;
    }
    str = str.substring(0, str.lastIndexOf(","));
    return str;
  }

  public void setAlertSystemLog(UpdateAlertSystemLog alertSystemLog) {
    
    this.alertSystemLog=alertSystemLog;
    
  }

  public void setMessageFormatter(MessageFormatter messageFormatter) {
   this.messageFormatter = messageFormatter;
  }

  

}
